<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 卡片开始 -->
      <el-card class="box-card">

        <!-- 头部导航栏 -->
        <el-tabs v-model="activeName">
          <!-- 组织架构标签开始 -->
          <el-tab-pane label="组织架构" name="first">

            <!-- 分栏框架 -->
            <el-row type="flex" justify="space-between" align="middle" class="department-header">
              <!-- 头部区域分栏 -->
              <el-col :span="20">
                <i class="el-icon-s-home" />
                <span class="company">江苏传智播客教育科技股份有限公司</span>

              </el-col>
              <!-- 头部区域分栏-结束 -->

              <!-- 头部区域--操作按钮分栏 -->
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <el-col :span="20">负责人</el-col>
                  <el-col :span="4">
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        操作<i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDept()">添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                      <!-- 下拉菜单--结束 -->
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
              <!-- 头部区域--操作按钮分栏 结束-->

            </el-row>
            <!-- 分栏框架 结束-->
            <hr>
            <!-- 树形结构开始 -->
            <el-tree
              :data="data"
              :props="defaultProps"
              node-key="id"
              default-expand-all
              :expand-on-click-node="false"
              @node-click="handleNodeClick"
            >
              <template slot-scope="{ data }">
                <el-row type="flex" justify="space-between" style="height: 50px; width: 100%;" align="middle">
                  <el-col :span="20">
                    <span>{{ data.name }}</span>
                  </el-col>

                  <el-col :span="4">
                    <el-row type="flex" justify="end">
                      <el-col>{{ data.manager }}</el-col>
                      <el-dropdown>
                        <span class="el-dropdown-link">
                          操作<i class="el-icon-arrow-down el-icon--right" />
                        </span>
                        <el-dropdown-menu slot="dropdown">
                          <el-dropdown-item @click.native="addDept(data.id)">添加子部门</el-dropdown-item>
                          <el-dropdown-item @click.native="editDept(data.id)">编辑部门</el-dropdown-item>
                          <el-dropdown-item v-if="data&&!data.children" @click.native="delDept(data.id)">删除部门</el-dropdown-item>
                        </el-dropdown-menu>
                      </el-dropdown>
                    </el-row>
                  </el-col>
                </el-row>
              </template>

            </el-tree>
            <!-- 树形结构结束 -->

          </el-tab-pane>
          <!-- 组织架构 -->
        </el-tabs>
        <!-- 头部导航栏 结束-->
      </el-card>
      <!-- 卡片结束 -->

      <!-- 添加部门时候的弹框 -->
      <el-dialog
        :title="isEdit?'编辑部门信息':'新增部门信息'"
        :visible.sync="centerDialogVisible"
        width="30%"
        :close-on-click-modal="false"
        :before-close="handleClose"
      >
        <!-- 弹窗表单 -->
        <deptDialog
          ref="editForm"
          :employee-simple="employeeSimple"
          :center-dialog-visible.sync="centerDialogVisible"
          :origin-list="originList"
          :is-edit="isEdit"
          :click-depart-id="clickDepartId"
          @passForm="getForm"
        />
        <!-- :center-dialog-visible.sync="centerDialogVisible"用于组件(父)传值,注意.sync,这是父子组件双向绑定 -->
        <!-- :origin-list="originList"用于将 originList传入子组件进行非重校验-->
      </el-dialog>
      <!-- 添加部门时候的弹框--结束 -->

    </div>
  </div>
</template>

<script>

import { getDepartmentsAPI, getEmployeeSimpleAPI, addDepartmentsAPI, delCompanyDepartmentAPI, getCompanyDepartmentByIdAPI, editDeptAPI } from '@/api/department'
import deptDialog from './deptDialog.vue'

// 树形数据生成函数,用于将后端数据处理成树形
import { transTree } from '@/utils/index'
export default {
  // 注册组件
  components: {
    deptDialog
  },
  data() {
    return {
      // 控制默认打开的标签卡
      activeName: 'first',

      // 树形结构的数据
      data: [],

      // 树形结构数据的显示
      defaultProps: {
        children: 'children',
        label: 'label'
      },

      // 控制添加部门时候的弹窗展示与否
      centerDialogVisible: false,

      // 部门负责人信息
      employeeSimple: {},

      // 被点击的部门的pid
      clickDepartId: '',

      // 区分添加\编辑按钮
      isEdit: false,

      // 只有4对key:value的部门信息,传进子组件用于做非重校验
      originList: {}
    }
  },
  created() {
    this.getDepartmentsFn()
    this.getEmployeeSimpleFn()
  },
  methods: {
    // 树形结构被点击
    handleNodeClick(data) {
      // console.log(data)
    },

    // 获得部门结构
    async getDepartmentsFn() {
      const res = await getDepartmentsAPI()
      this.data = transTree(res.data.depts, '')
      // console.log(res.data.depts)
      // console.log(transTree())
      // console.log(this.data)

      // 将数据存入originList一份,用于做非重校验
      this.originList = res.data.depts.map(item => ({
        id: item.id,
        code: item.code,
        pid: item.pid,
        name: item.name
      }))
    },

    // 获取部门负责人
    async getEmployeeSimpleFn() {
      const res = await getEmployeeSimpleAPI()
      this.employeeSimple = res.data
      // console.log(res)
    },

    // 添加部门功能
    addDept(id) {
      // 将添加控制变量置为flase
      this.isEdit = false
      // console.log(id)
      // 展示弹窗
      this.centerDialogVisible = true
      // 保存被点击的pid
      this.clickDepartId = id
    },

    // 弹框的确认按钮\\表单填完子组件传回,调用api提交新部门,关闭弹窗
    async getForm(form) {
      console.log(form)
      // 判断发送编辑API还是添加的API(通过isEdit变量来判断)
      let res
      if (this.isEdit) {
        res = await editDeptAPI(form)
      } else {
        form.pid = this.clickDepartId
        res = await addDepartmentsAPI(form)
      }

      // console.log(res)
      if (res.success) {
        this.$message.success(res.message)
        // 关闭弹窗
        this.centerDialogVisible = false
        // 刷新页面
        this.getDepartmentsFn()
      }
    },

    // 删除部门功能
    delDept(id) {
      this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // console.log(id)
        const res = await delCompanyDepartmentAPI(id)
        // console.log(res)
        if (res.success) {
          this.$message.success(res.message)
          // 刷新页面
          this.getDepartmentsFn()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    // 编辑部门功能
    async editDept(id) {
      // 将编辑控制变量置为true
      this.isEdit = true
      // 将被点击的变量的id保存下来
      this.clickDepartId = id
      // 展示弹窗
      this.centerDialogVisible = true
      // 信息回填
      const res = await getCompanyDepartmentByIdAPI(id)
      // console.log(res)
      this.$refs.editForm.form = res.data
    },

    // 弹框关闭时会触发该事件
    handleClose(done) {
      // 重置表单
      this.$refs.editForm.form = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
      // 固有形参,表示继续关闭
      done()
    }
  }
}
</script>

<style lang="scss" scoped>

  .box-card {
    padding: 10px 30px;
  }

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}
</style>
